package com.jlnk.rxjavademo.presenter;

import android.app.Activity;
import android.Manifest;
import androidx.core.content.ContextCompat;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.media.MediaScannerConnection;
import android.os.Environment;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class ImageDetailPresenter {
    private ImageDetailView imageDetailView;
    private String imageUrl;

    public ImageDetailPresenter(ImageDetailView imageDetailView, String imageUrl) {
        this.imageDetailView = imageDetailView;
        this.imageUrl = imageUrl;
    }

    public void loadImage() {
        imageDetailView.showLoading();
        Glide.with(imageDetailView.getContext())
                .load(imageUrl)
                .into(imageDetailView.getImageView());
        imageDetailView.hideLoading();
    }

    public void saveImageToGallery() {
        Glide.with(imageDetailView.getContext())
                .asBitmap()
                .load(imageUrl)
                .into(new CustomTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                        saveBitmap(resource);
                    }

                    @Override
                    public void onLoadCleared(@Nullable Drawable placeholder) {
                    }

                    @Override
                    public void onLoadFailed(@Nullable Drawable errorDrawable) {
                    }
                });
    }

    private void saveBitmap(Bitmap bitmap) {
        Context context = imageDetailView.getContext();
        if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) !=
                PackageManager.PERMISSION_GRANTED) {
            ((Activity) context).requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    1);
            return;
        }

        String fileName = "JPEG_" + System.currentTimeMillis() + ".jpg";
        File picturesDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        if (!picturesDirectory.exists()) {
            picturesDirectory.mkdirs();
        }
        File imageFile = new File(picturesDirectory, fileName);

        try (FileOutputStream out = new FileOutputStream(imageFile)) {
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
            MediaScannerConnection.scanFile(context, new String[]{imageFile.getAbsolutePath()}, null, null);
            imageDetailView.showSaveSuccess();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public interface ImageDetailView {
        void showLoading();
        void hideLoading();
        ImageView getImageView();
        Context getContext();
        void showSaveSuccess();
    }
}